package com.zegome.game.gomestory.main.scene.play.board;

import java.util.Random;

import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.zegome.game.entity.sprite.BaseSprite;

public class BgPikachu extends BaseSprite{
	Board board;
	int[][] map;
	Color color;
	
	public BgPikachu(Board board){
		super(0, 0);
		this.board = board;		
		map = new int[10][14];
	}
	
	public void getMap(){		
		for(int i=0;i<10;i++){
			for(int j=0;j<14;j++){
				map[i][j] = board.dataMap.map[i][j].type;
			}
		}
		Random rand = new Random();
		switch (rand.nextInt(5)) {
		case 0:
			color = Color.BLUE;
			break;
		case 1:			
			color = Color.CYAN;
			break;
		case 2:			
			color = Color.GRAY;
			break;
		case 3:			
			color = Color.GREEN;
			break;
		case 4:			
			color = Color.MAGENTA;
			break;
		case 5:			
			color = Color.ORANGE;			
			break;
		case 6:			
			color = Color.PINK;
			break;
		case 7:			
			color = Color.RED;
			break;
		case 8:			
			color = Color.CLEAR;
			break;
		case 9:			
			color = Color.YELLOW;
			break;
		case 10:			
			color = Color.WHITE;
			break;
		}
	}
	
	int row,col;
	float mX, mY;
	boolean isLeft, isTop, isRight, isBottom;
	int total;
	float wiTp,hiTp;
	
	public void onDraw(final SpriteBatch pSpriteBatch, final float pAlpha) {
		
		pSpriteBatch.setColor(color.r, color.g, color.b, 0.50f);
	
//		mX = board.dataMap.xBegin+20;
//		mY = board.dataMap.yBegin+20;
//		wiTp = 100;
//		hiTp = 100;				
//		pSpriteBatch.draw(board.screen.ctr.a.bgConner2, mX-board.dataMap.wiEdgeVer, mY, wiTp/2, hiTp/2, wiTp, hiTp,1f,1f,90);	
		
		for(int i=1;i<=board.dataMap.mapRow;i++){
			for(int j=1;j<=board.dataMap.mapCol;j++){			
				
				if(map[i][j]>=0){
				mX = board.dataMap.wi1Pikachu*j+board.dataMap.xBegin;
				mY = board.dataMap.hi1Pikachu*i+board.dataMap.yBegin;
				
				isLeft = false;
				isBottom = false;
				isTop = false;
				isRight = false;
				total = 0;
				
				row = i;
				col = j;
								
				if(map[row][col-1]<0){
					isLeft = true;
					total++;
//					if(board.dataMap.map[row+1][col].type<0){
//						if(board.dataMap.map[row-1][col].type<0){						
//							pSpriteBatch.draw(board.screen.ctr.a.eL, mX-board.dataMap.wiEdgeVer, mY+board.dataMap.hiEdgeVerUnit, board.dataMap.wiEdgeVer, board.dataMap.hiEdgeVerUnit*7);				
//						}
//						else
//							pSpriteBatch.draw(board.screen.ctr.a.eL, mX-board.dataMap.wiEdgeVer, mY, board.dataMap.wiEdgeVer, board.dataMap.hiEdgeVerUnit*8);				
//					}
//					else if(board.dataMap.map[row-1][col].type<0){
//						pSpriteBatch.draw(board.screen.ctr.a.eL, mX-board.dataMap.wiEdgeVer, mY+board.dataMap.hiEdgeVerUnit, board.dataMap.wiEdgeVer, board.dataMap.hiEdgeVerUnit*8);				
//					}
//					else{
//						pSpriteBatch.draw(board.screen.ctr.a.eL, mX-board.dataMap.wiEdgeVer, mY, board.dataMap.wiEdgeVer, board.dataMap.hiEdgeVerUnit*9);				
//					}
					
				}
				
				if(map[row][col+1]<0){
					isRight = true;
					total++;					
//					if(board.dataMap.map[row+1][col].type<0){
//						if(board.dataMap.map[row-1][col].type<0){						
//							pSpriteBatch.draw(board.screen.ctr.a.eR, mX+board.dataMap.wi1Pikachu, mY+board.dataMap.hiEdgeVerUnit, board.dataMap.wiEdgeVer, board.dataMap.hiEdgeVerUnit*7);				
//						}
//						else
//							pSpriteBatch.draw(board.screen.ctr.a.eR, mX+board.dataMap.wi1Pikachu, mY, board.dataMap.wiEdgeVer, board.dataMap.hiEdgeVerUnit*8);				
//					}
//					else if(board.dataMap.map[row-1][col].type<0){
//						pSpriteBatch.draw(board.screen.ctr.a.eR, mX+board.dataMap.wi1Pikachu, mY+board.dataMap.hiEdgeVerUnit, board.dataMap.wiEdgeVer, board.dataMap.hiEdgeVerUnit*8);				
//					}
//					else{
//						pSpriteBatch.draw(board.screen.ctr.a.eR, mX+board.dataMap.wi1Pikachu, mY, board.dataMap.wiEdgeVer, board.dataMap.hiEdgeVerUnit*9);				
//					}
					
				}
				
				if(map[row+1][col]<0){
					isTop = true;
					total++;
//					if(board.dataMap.map[row][col+1].type<0){
//						if(board.dataMap.map[row][col-1].type<0){						
//							pSpriteBatch.draw(board.screen.ctr.a.eT, mX+board.dataMap.wiEdgeHorUnit, mY+board.dataMap.hi1Pikachu, board.dataMap.wiEdgeHorUnit*7, board.dataMap.wiEdgeVer);				
//						}
//						else
//							pSpriteBatch.draw(board.screen.ctr.a.eT, mX, mY+board.dataMap.hi1Pikachu, board.dataMap.wiEdgeHorUnit*8, board.dataMap.wiEdgeVer);				
//					}
//					else if(board.dataMap.map[row][col-1].type<0){
//						pSpriteBatch.draw(board.screen.ctr.a.eT, mX+board.dataMap.wiEdgeHorUnit, mY+board.dataMap.hi1Pikachu, board.dataMap.wiEdgeHorUnit*8, board.dataMap.wiEdgeVer);				
//					}
//					else{
//						pSpriteBatch.draw(board.screen.ctr.a.eT, mX, mY+board.dataMap.hi1Pikachu, board.dataMap.wiEdgeHorUnit*9, board.dataMap.wiEdgeVer);				
//					}					
				}
				
				if(map[row-1][col]<0){
					isBottom = true;
					total++;
//					if(board.dataMap.map[row][col+1].type<0){
//						if(board.dataMap.map[row][col-1].type<0){						
//							pSpriteBatch.draw(board.screen.ctr.a.eB, mX+board.dataMap.wiEdgeHorUnit, mY-board.dataMap.wiEdgeVer, board.dataMap.wiEdgeHorUnit*7, board.dataMap.wiEdgeVer);				
//						}
//						else
//							pSpriteBatch.draw(board.screen.ctr.a.eB, mX, mY-board.dataMap.wiEdgeVer, board.dataMap.wiEdgeHorUnit*8, board.dataMap.wiEdgeVer);				
//					}
//					else if(board.dataMap.map[row][col-1].type<0){
//						pSpriteBatch.draw(board.screen.ctr.a.eB, mX+board.dataMap.wiEdgeHorUnit, mY-board.dataMap.wiEdgeVer, board.dataMap.wiEdgeHorUnit*8, board.dataMap.wiEdgeVer);				
//					}
//					else{
//						pSpriteBatch.draw(board.screen.ctr.a.eB, mX, mY-board.dataMap.wiEdgeVer, board.dataMap.wiEdgeHorUnit*9, board.dataMap.wiEdgeVer);				
//					}					
				}
			
				if(total == 4){
					pSpriteBatch.draw(board.screen.ctr.a.bg4Gome, mX-board.dataMap.wiEdgeVer, mY-board.dataMap.wiEdgeVer, board.dataMap.wi1Pikachu+board.dataMap.wiEdgeVer*2, board.dataMap.hi1Pikachu+board.dataMap.wiEdgeVer*2);				
				}
				else if(total == 3){
					if(isTop == false){
						wiTp = board.dataMap.wi1Pikachu+board.dataMap.wiEdgeVer*2;
						hiTp = board.dataMap.hi1Pikachu+board.dataMap.wiEdgeVer;
						pSpriteBatch.draw(board.screen.ctr.a.bg2WT, mX-board.dataMap.wiEdgeVer, mY-board.dataMap.wiEdgeVer,wiTp/2,hiTp/2, wiTp, hiTp,1f,1f,0);				
					}
					else if(isBottom == false){
						wiTp = board.dataMap.wi1Pikachu+board.dataMap.wiEdgeVer*2;
						hiTp = board.dataMap.hi1Pikachu+board.dataMap.wiEdgeVer;
						pSpriteBatch.draw(board.screen.ctr.a.bg2WB, mX-board.dataMap.wiEdgeVer, mY,wiTp/2, hiTp/2, wiTp, hiTp,1f,1f,0);				
					}
					else if(isLeft == false){
						wiTp = board.dataMap.wi1Pikachu+board.dataMap.wiEdgeVer;
						hiTp = board.dataMap.hi1Pikachu+board.dataMap.wiEdgeVer*2;
						pSpriteBatch.draw(board.screen.ctr.a.bg2WL, mX, mY-board.dataMap.wiEdgeVer, wiTp/2, hiTp/2, wiTp, hiTp,1f,1f,0);
					}
					else if(isRight == false){
						wiTp = board.dataMap.wi1Pikachu+board.dataMap.wiEdgeVer;
						hiTp = board.dataMap.hi1Pikachu+board.dataMap.wiEdgeVer*2;
						pSpriteBatch.draw(board.screen.ctr.a.bg2WR, mX-board.dataMap.wiEdgeVer, mY-board.dataMap.wiEdgeVer, wiTp/2, hiTp/2, wiTp, hiTp,1f,1f,0);			
					}
				}
				else if(total == 2){
					if(isTop && isLeft){			
						wiTp = board.dataMap.wi1Pikachu+board.dataMap.wiEdgeVer;
						hiTp = board.dataMap.hi1Pikachu+board.dataMap.wiEdgeVer;
						pSpriteBatch.draw(board.screen.ctr.a.bg1TL, mX-board.dataMap.wiEdgeVer, mY, wiTp/2, hiTp/2, wiTp, hiTp,1f,1f,0);		
					}
					else if(isTop && isRight){
						wiTp = board.dataMap.wi1Pikachu+board.dataMap.wiEdgeVer;
						hiTp = board.dataMap.hi1Pikachu+board.dataMap.wiEdgeVer;
						pSpriteBatch.draw(board.screen.ctr.a.bg1TR, mX, mY, wiTp/2, hiTp/2, wiTp, hiTp,1f,1f,0);		
					}
					else if(isBottom && isTop){
						wiTp = board.dataMap.wi1Pikachu;
						hiTp = board.dataMap.hi1Pikachu+board.dataMap.wiEdgeVer*2;
						pSpriteBatch.draw(board.screen.ctr.a.bg0Gome, mX, mY-board.dataMap.wiEdgeVer, wiTp, hiTp);	
					}
					else if(isBottom && isLeft){
						wiTp = board.dataMap.wi1Pikachu+board.dataMap.wiEdgeVer;
						hiTp = board.dataMap.hi1Pikachu+board.dataMap.wiEdgeVer;
						pSpriteBatch.draw(board.screen.ctr.a.bg1BL, mX-board.dataMap.wiEdgeVer, mY-board.dataMap.wiEdgeVer, wiTp/2, hiTp/2, wiTp, hiTp,1f,1f,0);		
					}
					else if(isBottom && isRight){
						wiTp = board.dataMap.wi1Pikachu+board.dataMap.wiEdgeVer;
						hiTp = board.dataMap.hi1Pikachu+board.dataMap.wiEdgeVer;
						pSpriteBatch.draw(board.screen.ctr.a.bg1BR, mX, mY-board.dataMap.wiEdgeVer, wiTp/2, hiTp/2, wiTp, hiTp,1f,1f,0);		
					}
					else if(isLeft && isRight){
						wiTp = board.dataMap.wi1Pikachu+board.dataMap.wiEdgeVer*2;
						hiTp = board.dataMap.hi1Pikachu;
						pSpriteBatch.draw(board.screen.ctr.a.bg0Gome, mX-board.dataMap.wiEdgeVer, mY, wiTp, hiTp);		
					}
				}
				else if(total ==1){
					if(isTop){
						wiTp = board.dataMap.wi1Pikachu;
						hiTp = board.dataMap.hi1Pikachu+board.dataMap.wiEdgeVer;
						pSpriteBatch.draw(board.screen.ctr.a.bg0Gome, mX, mY, wiTp, hiTp);	
					}
					else if(isBottom){
						wiTp = board.dataMap.wi1Pikachu;
						hiTp = board.dataMap.hi1Pikachu+board.dataMap.wiEdgeVer;
						pSpriteBatch.draw(board.screen.ctr.a.bg0Gome, mX, mY-board.dataMap.wiEdgeVer, wiTp, hiTp);	
					}
					else if(isLeft){
						wiTp = board.dataMap.wi1Pikachu+board.dataMap.wiEdgeVer;
						hiTp = board.dataMap.hi1Pikachu;
						pSpriteBatch.draw(board.screen.ctr.a.bg0Gome, mX-board.dataMap.wiEdgeVer, mY, wiTp, hiTp);	
					}
					else if(isRight){
						wiTp = board.dataMap.wi1Pikachu+board.dataMap.wiEdgeVer;
						hiTp = board.dataMap.hi1Pikachu;
						pSpriteBatch.draw(board.screen.ctr.a.bg0Gome, mX, mY, wiTp, hiTp);	
					}
				}
				else{
					wiTp = board.dataMap.wi1Pikachu;
					hiTp = board.dataMap.hi1Pikachu;
					pSpriteBatch.draw(board.screen.ctr.a.bg0Gome, mX, mY, wiTp, hiTp);	
				}
			}
				else if(map[i][j]==-1){
					mX = board.dataMap.wi1Pikachu*j+board.dataMap.xBegin;
					mY = board.dataMap.hi1Pikachu*i+board.dataMap.yBegin;
					
					isLeft = false;
					isBottom = false;
					isTop = false;
					isRight = false;
					total = 0;
					
					row = i;
					col = j;
									
					if(map[row][col-1]<0){
						isLeft = true;
					}
					
					if(map[row][col+1]<0){
						isRight = true;
					}
					
					if(map[row+1][col]<0){
						isTop = true;			
					}
					
					if(map[row-1][col]<0){
						isBottom = true;									
					}
					if(i-1>=1 && j-1>=1 && isTop == false && isLeft == false){
						pSpriteBatch.draw(board.screen.ctr.a.conTL, mX, mY, board.dataMap.wi1Pikachu, board.dataMap.hi1Pikachu);
					}
				}
			}
		}
	}
}
